package com.hw.paltfrom.service.mainscreen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwBaseOverrunstationMapper;
import com.hw.paltfrom.mapper.HwDataOverrunstationHistoryMapper;
import com.hw.paltfrom.mapper.HwDataOverrunstationMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.HwBaseOverrunstation;
import com.hw.paltfrom.pojo.HwDataOverrunstation;
import com.hw.paltfrom.pojo.HwDataOverrunstationHistory;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.DateTimeUtil;
import com.hw.paltfrom.utils.byteUtils;

@Service
public class OverRunStationDimeServer {

	@Autowired
	private HwDataOverrunstationHistoryMapper hdosMapper;
	@Autowired
	private HwBaseOverrunstationMapper hbosMapper;

	public Map<String, Object> query() {

		HashMap<String, Object> result = new HashMap<String, Object>();

		// 三维数组x：所有超限站名称
		Wrapper<HwBaseOverrunstation> stationNamewrapper = new EntityWrapper<HwBaseOverrunstation>();
		stationNamewrapper.eq("F_DeleteMark", "0").eq("F_EnabledMark", "1");
		List<HwBaseOverrunstation> overRunStationList = hbosMapper.selectList(stationNamewrapper);
		ArrayList<String> names = new ArrayList<String>();
		for (HwBaseOverrunstation name : overRunStationList) {
//			names.add(name.getfOverrunname());
			names.add(name.getfSimplespelling());
		}
		result.put("StationNames", names);

		// 首先判断当前缓存中是今天的数据？
		int flag = 0;
		long targetDate = byteUtils.targetDate;
		boolean istoday = DateTimeUtil.handleDate(targetDate);
		if (istoday) {
			if (StringUtils.isNotBlank(byteUtils.byteData[0][0])) {
				flag = 1;
			}
		}

		if (flag == 1) {
			// 使用缓存

		} else {
			// 覆盖缓存

			// 更新时间
//			byteUtils.targetDate = System.currentTimeMillis();

			// 算缓存
			// 超限站数量
			int size = overRunStationList.size();
			Integer[][] resultbyte = new Integer[size * 7][3];
			int forNum=0;
			for (int i = 0; i < size; i++) {
				HwBaseOverrunstation oldBaseStation = overRunStationList.get(i);
				String overruncode = oldBaseStation.getfOverruncode();
				for (int n = 0; n < 7; n++) {
					
//					x值相同，就是当前站 7天的数据
					HashMap<String, Long> afterTimeRound = DateTimeUtil.getAfterTimeRound(n);
					
					Timestamp endTime =new Timestamp(afterTimeRound.get("endTime")) ;
					Timestamp startTime =new Timestamp(afterTimeRound.get("startTime")) ;
					
					Wrapper<HwDataOverrunstationHistory> stationCountwrapper=new EntityWrapper<HwDataOverrunstationHistory>();
					stationCountwrapper.eq("F_OverrunCode", overruncode);
					stationCountwrapper.between("D_JCSJ", startTime, endTime);
					Integer selectCount = hdosMapper.selectCount(stationCountwrapper);
					
//					X值
					resultbyte[forNum][0]=n;
					
//					y值
					resultbyte[forNum][1]=i;
//					z值
					resultbyte[forNum][2]=selectCount;
					forNum++;
//					System.out.println("F_OverrunCode:  "+overruncode+"   D_JCSJ:"+startTime+" "+endTime+" 数量："+selectCount);
//					System.out.println(Arrays.deepToString(resultbyte));
				}
			}
//			String string = Arrays.deepToString(resultbyte);
			result.put("Stationdata", resultbyte);
			// 写入缓存

		}

		 return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
	}

//	public static void main(String[] args) {
//		String[][] str = new String[3][3];
//		str[0][0] = "1";
//		str[0][1] = "2";
//		str[0][2] = "3";
//		String string = Arrays.deepToString(str);
//		System.out.println(string);
//	}

}
